package com.hakgc.rent.dao.impl;

import com.hakgc.rent.dao.BaseDao;
import com.hakgc.rent.dao.DistrictDao;
import com.hakgc.rent.entity.District;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Xing.Jiejian
 * @date 2020/10/16
 */
public class DistrictDaoImpl extends BaseDao implements DistrictDao {

    @Override
    public boolean save(District district) {
        String sql = "INSERT INTO hos_district(dName) VALUES(?)";
        return executeUpdate(sql, district.getName()) > 0;
    }

    @Override
    public boolean update(District district) {
        // 使用"?"作为绑定参数的占位符
        String sql = "UPDATE hos_district SET dName=? WHERE did=?";
        return executeUpdate(sql, district.getName(), district.getId()) > 0;
    }

    @Override
    public boolean deleteById(int id) {
        String sql = "DELETE FROM hos_district WHERE did=?";
        return executeUpdate(sql, id) > 0;
    }

    @Override
    public District findById(int id) {
        String sql = "SELECT did, dName FROM hos_district WHERE did=?";
        List<District> districts = getListFromRS(executeQuery(sql, id));
        if (districts != null && districts.size() > 0) {
            return districts.get(0);
        }
        return null;
    }

    @Override
    public List<District> findAll() {
        String sql = "SELECT did, dName FROM hos_district";
        return getListFromRS(executeQuery(sql));
    }

    @Override
    public List<District> findByLimit(int offset, int limit) {
        String sql = "SELECT did, dName FROM hos_district LIMIT ?,?";
        return getListFromRS(executeQuery(sql, offset, limit));
    }

    @Override
    public List<District> findByLimit(String likeName, int offset, int limit) {
        String sql = "SELECT did, dName FROM hos_district WHERE dName LIKE ? LIMIT ?,?";
        return getListFromRS(executeQuery(sql, "%"+likeName+"%",offset, limit));
    }

    @Override
    public int getAllCount() {
        String sql = "SELECT COUNT(did) FROM hos_district";
        return (int) (long) uniqueResult(sql);
    }

    @Override
    public int getAllCount(String likeName) {
        String sql = "SELECT COUNT(did) FROM hos_district WHERE dName LIKE ?";
        return (int) (long) uniqueResult(sql, "%"+likeName+"%");
    }

    /**
     * 从结果集中获取街道集合
     *
     * @param rs 包含街道的结果集
     * @return 街道集合
     */
    private List<District> getListFromRS(ResultSet rs) {
        List<District> districts = null;
        try {
            if (rs != null) {
                districts = new ArrayList<>();
                while (rs.next()) {
                    District district = new District();
                    district.setId(rs.getInt("did"));
                    district.setName(rs.getString("dName"));
                    districts.add(district);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 注意：解析完结果集之后一定要按照顺序关闭所有数据库对象
            closeAll();
        }
        return districts;
    }

}
